#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int findMinArrowShots(vector<vector<int>>& points) {
    int ans = 1;
    sort(points.begin(), points.end());
    int left = points[0][0], right = points[0][1];
    for (int i = 1; i < points.size(); i++)
    {
        if (points[i][0] <= right)
        {
            left = max(left, points[i][0]);
            right = min(right, points[i][1]);
        }
        else
        {
            ans++;
            left = points[i][0], right = points[i][1];
        }
    }
    return ans;
}

int maxSumDivThree(vector<int>& nums) {
    /*int ret = 0;
    for (auto x : nums)
        ret += x;
    if (ret % 3 == 0) return ret;
    else if (ret % 3 == 1)
    {
        int flag2 = 0;
        for (auto x : nums)
            if (x % 3 == 2) flag2++;
        if ((flag2 - 2) * 2 % 3 == 0)
        {
            int min = INT_MAX;
            for (auto x : nums)
                if (x % 3 == 1 && x < min) min = x;
            ret -= min;
        }
        else
        {
            int x1 = INT_MAX, x2 = INT_MAX;
            for (auto x : nums)
            {
                if (x < x1) x2 = x1, x1 = x;
                else if (x < x2) x2 = x;
            }
            ret = ret - x1 - x2;
        }
    }
    else
    {
        int flag1 = 0;
        for (auto x : nums)
            if (x % 3 == 1) flag1++;
        if (flag1 % 3 == 2)
        {
            int x1 = INT_MAX, x2 = INT_MAX;
            for (auto x : nums)
            {
                if (x < x1) x2 = x1, x1 = x;
                else if (x < x2) x2 = x;
            }
            ret = ret - x1 - x2;
        }
        else
        {
            int min = INT_MAX;
            for (auto x : nums)
                if (x % 3 == 2 && x < min) min = x;
            ret -= min;
        }
    }
    return ret;*/

    int ret = 0, x1 = INT_MAX, x2 = INT_MAX, y1 = INT_MAX, y2 = INT_MAX;
    for (auto x : nums)
    {
        ret += x;
        if (x % 3 == 1)
        {
            if (x < x1) x2 = x1, x1 = x;
            else if (x < x2) x2 = x;
        }
        if (x % 3 == 2)
        {
            if (x < y1) y2 = y1, y1 = x;
            else if (x < y2) y2 = x;
        }
    }
    if (ret % 3 == 0) return ret;
    else if (ret % 3 == 1) return max(ret - x1, ret - y1 - y2);
    else return max(ret - y1, ret - x1 - x2);
}

int main()
{
    int a = INT_MAX;
	vector<int> vv{ 3,6,5,1,8 };
	/*sort(vv.begin(), vv.end(), [&](const vector<int>& i, const vector<int>& j) {
		return i[1] < j[1];
		});*/
    //findMinArrowShots(vv);
    int x = maxSumDivThree(vv);
	return 0;
}